package cn.gov.pbc.gd.credit.core.service;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import com.liu.jbase.ResultVo;
import com.liu.tools.LogUtil;
import com.liu.tools.NetHelper;

import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.user.service.realuser.IRealUserService;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.query.ReqCCApplyTblVo;
import cn.gov.pbc.gd.credit.vo.center.query.ReqFirstStepVo;
import cn.gov.pbc.gd.credit.vo.center.query.ReqSecondStepVo;
import cn.gov.pbc.gd.credit.vo.inquiry.InquiryVo;
import cn.gov.pbc.gd.credit.vo.inquiry.agent.AgentVo;
import cn.gov.pbc.gd.credit.vo.realuser.ResRealUserVo;

/**
 * @author liuxingmi
 * @datetime 2017年6月7日 下午9:20:42
 * @desc 查询工具类
 */
public class InquiryUtil {

	private static Logger logger = org.slf4j.LoggerFactory.getLogger(InquiryUtil.class);
	
	/**
	 * @author liuxingmi
	 * @datetime 2017年6月12日 下午3:11:12
	 * @desc 个人查询
	 * @param vo
	 * @return ReqFirstStepVo
	 */
	public static ReqFirstStepVo getReqFirstStepVo(InquiryVo vo){
		
		ReqFirstStepVo reqFirstStepVo = new ReqFirstStepVo();
		reqFirstStepVo.setForwardtype("4");
		reqFirstStepVo.setNexttype("1");
		reqFirstStepVo.setQuerytype(getQueryType(vo.getFormat()));
		reqFirstStepVo.setCtype(getCardType(vo.getCardType()));
		reqFirstStepVo.setIdno(vo.getCardNum());
		reqFirstStepVo.setUser(vo.getUserName());

		if(vo.getAgentVo() != null){
			
			AgentVo agentVo = vo.getAgentVo();
			reqFirstStepVo.setRelcertno(agentVo.getAgentCardNum());
			reqFirstStepVo.setRelcerttype(getCardType(agentVo.getAgentCardType()));
			reqFirstStepVo.setRelname(agentVo.getAgentName());	
			reqFirstStepVo.setReltel(agentVo.getPhone());
		}
		
		return reqFirstStepVo;
	} 

	/**
	 * @author liuxingmi
	 * @datetime 2017年6月12日 下午3:11:03
	 * @desc 代理查询
	 * @param vo
	 * @return ReqFirstStepVo
	 */
	public static ReqFirstStepVo getReqFirstStepForAgent(AgentVo vo){
		
		ReqFirstStepVo reqFirstStepVo = new ReqFirstStepVo();
		reqFirstStepVo.setForwardtype("4");
		reqFirstStepVo.setNexttype("1");
		reqFirstStepVo.setQuerytype(getQueryType(vo.getFormat()));
		reqFirstStepVo.setCtype(getCardType(vo.getCardType()));
		reqFirstStepVo.setIdno(vo.getCardNum());
		reqFirstStepVo.setUser(vo.getUserName());

		reqFirstStepVo.setRelcertno(vo.getAgentCardNum());
		reqFirstStepVo.setRelcerttype(getCardType(vo.getAgentCardType()));
		reqFirstStepVo.setRelname(vo.getAgentName());	
		reqFirstStepVo.setReltel(vo.getPhone());
		
		return reqFirstStepVo;
	} 
	
	public static ReqCCApplyTblVo getApplyTbl(ReqSecondStepVo vo){
		
		ReqCCApplyTblVo tblVo = new ReqCCApplyTblVo();
		
		tblVo.setCtype(vo.getCerttype());
		tblVo.setIdno(vo.getCertno());
		tblVo.setQuerytype(CenterType.getName(vo.getQuerytype()));
		tblVo.setRelcertno(vo.getRelcertno());
		tblVo.setRelcerttype(CenterType.getName(vo.getCerttype()));
		tblVo.setRelname(vo.getRelname());
		tblVo.setReltel(vo.getReltel());
		tblVo.setUser(vo.getName());
		
		return tblVo;
	}
	
	public static String getQueryType(Integer format){
		
		if(format == null){
			return "23";
		}
		if(format == 1){
			return "21";
		} else if (format == 2) {
			return "24";
		} else {
			return "23";
		}
	}
	
	public static String getCardType(Integer cardType){
		//证件类型:1-身份证,2-户口簿,3-护照，4-军官证，5-士兵证, 6-港澳居民来往内地通行证，7-台湾同胞来往内地通行证，
		//8-临时身份证，9-外国人居住证，10-警官证，11-香港身份证，12-澳门身份证, 13-台湾身份证，14-其他证件
		
		switch (cardType) {
		case 1:	return "0";
		case 2:	return "1";
		case 3:	return "2";
		case 4:	return "3";
		case 5:	return "4";
		case 6:	return "5";		
		case 7:	return "6";		
		case 8:	return "7";		
		case 9:	return "8";		
		case 10: return "9";			
		case 11: return "A";			
		case 12: return "B";			
		case 13: return "C";			
		case 14: return "X";			
		default:
			return "X";
		}
	}
	
	
	public static ResCCUserVo getResCCUserVo(HttpServletRequest request, IRedisService redisService, IRealUserService realUserService){
		
		String sid = NetHelper.getReqParams(request, "sid", "");
		
		ResCCUserVo resCCUserVo = UserUtil.getRealUserLogin(sid, redisService);
		UserModel userModel = UserUtil.getUserModel(sid, redisService);
		if(resCCUserVo == null){
						
			resCCUserVo = loginCCUser(userModel, redisService, realUserService, request);
			
			return resCCUserVo;
		}
		//刷新缓存时间
		redisService.expire(userModel.getRealUserAccount() + "_" + userModel.getUserId(), ConfConstant.realUserCacheTime);
		return resCCUserVo;
	}
	

	public static ResCCUserVo loginCCUser(UserModel userModel, IRedisService redisService, IRealUserService realUserService, HttpServletRequest request){
				
		ResultVo<ResRealUserVo> resultRealUser = realUserService.login(userModel.getRealUserId(), userModel.getUserId(), request);
		logger.info(LogUtil.msg("InquiryUtil:loginCCUser", "征信中心cookies失效，重新登录，acc=" + userModel.getRealUserAccount()));
		if(! CodeConstant.SUCCESS.equals(resultRealUser.getCode())){
			return null;
		}
		
		ResRealUserVo resRealUserVo = resultRealUser.getData();
		
		ResCCUserVo resCCUserVo = UserUtil.getRealUserLogin(resRealUserVo.getAccount(), userModel.getUserId(), redisService);
		
		return resCCUserVo;
	

	}

}
